#include "Man.h"

Man::Man(QWidget* parent) :
	_parent(parent)
{
	_jlad = new Join(parent); _jlau = new Join(parent); _jrad = new Join(parent);
	_jrau = new Join(parent); _jlld = new Join(parent); _jllu = new Join(parent);
	_jrld = new Join(parent); _jrlu = new Join(parent); _ju = new Join(parent); _jd = new Join(parent);

	_head = new Head(parent);
	_nlad = new Node(parent); _nlau = new Node(parent); _nrad = new Node(parent);
	_nrau = new Node(parent); _nlld = new Node(parent); _nllu = new Node(parent);
	_nrld = new Node(parent); _nrlu = new Node(parent);
	_nu = new Node(parent); _nm = new Node(parent); _nd = new Node(parent);

	_nlad->addJoin(_jlad);
	_nlau->addJoin(_jlad); _nlau->addJoin(_jlau);
	_nrad->addJoin(_jrad);
	_nrau->addJoin(_jrad); _nrau->addJoin(_jrau);
	_nlld->addJoin(_jlld);
	_nllu->addJoin(_jlld); _nllu->addJoin(_jllu);
	_nrld->addJoin(_jrld);
	_nrlu->addJoin(_jrld); _nrlu->addJoin(_jrlu);
	_nu->addJoin(_jlau); _nu->addJoin(_jrau); _nu->addJoin(_ju);
	_nm->addJoin(_ju); _nm->addJoin(_jd);
	_nd->addJoin(_jllu); _nd->addJoin(_jrlu); _nd->addJoin(_jd);

	_jlad->addNode(_nlad); _jlad->addNode(_nlau);
	_jlau->addNode(_nlau); _jlau->addNode(_nu);
	_jrad->addNode(_nrad); _jrad->addNode(_nrau);
	_jrau->addNode(_nrau); _jrau->addNode(_nu);
	_jlld->addNode(_nlld); _jlld->addNode(_nllu);
	_jllu->addNode(_nllu); _jllu->addNode(_nd);
	_jrld->addNode(_nrld); _jrld->addNode(_nrlu);
	_jrlu->addNode(_nrlu); _jrlu->addNode(_nd);
	_ju->addNode(_nu); _ju->addNode(_nm);
	_jd->addNode(_nm); _jd->addNode(_nd);
}

Man::~Man()
{
	_head->deleteLater();
	_nlad->deleteLater(); _nlau->deleteLater(); _nrad->deleteLater(); _nrau->deleteLater();
	_nlld->deleteLater(); _nllu->deleteLater(); _nrld->deleteLater(); _nrlu->deleteLater();
	_nu->deleteLater(); _nm->deleteLater(); _nd->deleteLater();
	_jlad->deleteLater(); _jlau->deleteLater(); _jrad->deleteLater(); _jrau->deleteLater();
	_jlld->deleteLater(); _jllu->deleteLater(); _jrld->deleteLater(); _jrlu->deleteLater();
	_ju->deleteLater(); _jd->deleteLater();
}

void Man::setVisible(bool visible)
{
	_head->setVisible(visible);
	_nlad->setVisible(visible); _nlau->setVisible(visible); _nrad->setVisible(visible); _nrau->setVisible(visible);
	_nlld->setVisible(visible); _nllu->setVisible(visible); _nrld->setVisible(visible); _nrlu->setVisible(visible);
	_nu->setVisible(visible); _nm->setVisible(visible); _nd->setVisible(visible);
	_jlad->setVisible(visible); _jlau->setVisible(visible); _jrad->setVisible(visible); _jrau->setVisible(visible);
	_jlld->setVisible(visible); _jllu->setVisible(visible); _jrld->setVisible(visible); _jrlu->setVisible(visible);
	_ju->setVisible(visible); _jd->setVisible(visible);
}

Head* Man::head()
{
	return _head;
}

void Man::setPosition(int x, int y)
{
	_x = x;
	_y = y;
	updateWidgets();
}

void Man::setRect(int w, int h)
{
	_w = w;
	_h = h;
	updateWidgets();
	_jlad->setRect(_x+_w,_y+_h);
	_jlau->setRect(_x+_w,_y+_h);
	_jrad->setRect(_x+_w,_y+_h);
	_jrau->setRect(_x+_w,_y+_h);
	_jlld->setRect(_x+_w,_y+_h);
	_jllu->setRect(_x+_w,_y+_h);
	_jrld->setRect(_x+_w,_y+_h);
	_jrlu->setRect(_x+_w,_y+_h);
	_ju->setRect(_x+_w,_y+_h);
	_jd->setRect(_x+_w,_y+_h);
}

void Man::updateWidgets()
{
	int baseX = 420;
	int baseY = 680;
	_head->setRect(_w*200/420, _w*200/420);//_h*200/680);
	int ndx = _w*40/420;
	int ndy = _h*40/680;
	int center = _w / 2;
	_nlad->setRect(ndx,ndy);_nlau->setRect(ndx,ndy); _nrad->setRect(ndx,ndy); _nrau->setRect(ndx,ndy);
	_nlld->setRect(ndx,ndy); _nllu->setRect(ndx,ndy); _nrld->setRect(ndx,ndy); _nrlu->setRect(ndx,ndy);
	_nu->setRect(ndx,ndy); _nm->setRect(ndx,ndy); _nd->setRect(ndx,ndy);

	_head->setPosition(_x + center - _w*100/420, _y);
	_nu->setPosition(_x + center - ndx/2, _y + _h*200/680 - ndy/2);
	_nm->setPosition(_x + center - ndx/2, _y + _h*330/680 - ndy/2);
	_nd->setPosition(_x + center - ndx/2, _y + _h*420/680 - ndy/2);
	_nlau->setPosition(_x + center - _w*60/420 - ndx/2, _y + _h*250/680 - ndy/2);
	_nrau->setPosition(_x + center + _w*60/420 - ndx/2, _y + _h*250/680 - ndy/2);
	_nlad->setPosition(_x + center - _w*150/420 - ndx/2, _y + _h*210/680 - ndy/2);
	_nrad->setPosition(_x + center + _w*150/420 - ndx/2, _y + _h*210/680 - ndy/2);
	_nllu->setPosition(_x + center - _w*60/420 - ndx/2, _y + _h*560/680 - ndy/2);
	_nrlu->setPosition(_x + center + _w*60/420 - ndx/2, _y + _h*560/680 - ndy/2);
	_nlld->setPosition(_x + center - _w*80/420 - ndx/2, _y + _h*640/680 - ndy/2);
	_nrld->setPosition(_x + center + _w*80/420 - ndx/2, _y + _h*640/680 - ndy/2);
}
